#include <stdio.h>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <unordered_map>
#include <map>

int table[255];
int numbers[] = {57, 71, 87, 97, 99, 101, 103, 113, 114, 115, 128, 129, 131, 137, 147, 156, 163, 186 };
std::string peptide_str = "GASPVTCINDKEMHFRYW";

std::map<int, int> spectr;

int str2int(std::string str)
{
	int result = 0;
	for (auto it=str.begin(); it!=str.end(); it++)
	{
		char ch = *it;
		result += table[ch];
	}
	return result;
}

std::map<int,int> getCyclopeptide(std::string input)
{
	int size = input.size();

	std::map<int,int> mylist;
	std::reverse(input.begin(), input.end());
	std::string dbl_str = input+input;
	for (int i=1; i<size; i++)
	{
		for (int j=0; j<size-i+1; j++)
		{
			std::string cur = dbl_str.substr(j, i);
			int current = str2int(cur);
			auto f = mylist.find(current);
			if (f == mylist.end())
				mylist.insert(std::pair<int,int>(current, 1));
			else
				f->second = f->second + 1;
		}
	}
	mylist.insert(std::pair<int,int>(0,1));
	mylist.insert(std::pair<int,int>(str2int(input),1));
	return mylist;
}

bool equal_spectres(std::map<int,int> a)
{
	int last;
	int s_last;
	for (auto it=a.begin(); it!=a.end(); it++)
	{
		last = it->first;
	}
	
	for (auto it=spectr.begin(); it!=spectr.end(); it++)
	{
		s_last = it->first;
	}
	
	if (last == s_last)
	{
		for (auto it=a.begin(); it!=a.end(); it++)
		{
			auto f = spectr.find(it->first);
			if (f != spectr.end())
			{
				if (f->second < it->second)
					return false;
			} else
				return false;
		}
		return true;
	} else
		return false;
}

bool contains_spectres(std::map<int,int> a)
{
	for (auto it=a.begin(); it!=a.end(); it++)
	{
		auto f = spectr.find(it->first);
		if (f != spectr.end())
		{
			if (f->second < it->second)
				return false;
		} else
			return false;
	}
	return true;
}

void expand(std::list<std::string>* from, std::list<std::string>* to)
{
	for (auto it=from->begin(); it!=from->end(); it++)
	{
		std::string cur = *it;
		for (auto ch=peptide_str.begin(); ch!=peptide_str.end(); ch++)
		{
			to->push_back(cur + *ch);
		}
	}
}



int main()
{
	FILE* file = fopen("input.txt", "r");
	FILE* table_file = fopen("integer_mass_table.txt", "r");
	FILE* out = fopen("result.txt", "w");
	for (int i=0; i<20; i++)
	{
		char c[15];
		int num;
		fscanf(table_file, "%s %d", &c, &num);
		table[c[0]] = num;
	}

	while (!feof(file))
	{
		int num;
		fscanf(file, "%d", &num);
		auto f = spectr.find(num);
		if (f == spectr.end())
			spectr.insert(std::pair<int,int>(num, 1));
		else
			f->second = f->second + 1;
	}
	auto res = getCyclopeptide("PTP");
	bool f = contains_spectres(res);
	std::list<std::string>* cur = new std::list<std::string>();
	std::list<std::string>* prev = new std::list<std::string>();
	prev->push_back("");
	
	while (!prev->empty())
	{
		expand(prev, cur);
		prev->clear();
		std::swap(cur, prev);
		for (auto it=prev->begin(); it!=prev->end(); it++)
		{
			std::map<int,int> pep = getCyclopeptide(*it);
			if (equal_spectres(pep))
			{
				std::string str = *it;
				for (int i=0; i<it->size()-1; i++)
				{
					fprintf(out,"%d-", table[str[i]]);
				}
				fprintf(out, "%d ", table[str[str.size()-1]]);
			} else if (contains_spectres(pep))
			{
				cur->push_back(*it);
			}
		}
		prev->clear();
		std::swap(cur, prev);
	}
	return 0;
}